package com.cheng.example.book.advise151.chap3.client43;
import lombok.Data;
@Data
public class Person implements Cloneable {
    public static void main(String[] args) {
        // 定义父亲
        Person f = new Person("父亲");
        // 定义大儿子
        Person s1 = new Person("大儿子", f);
        // 小儿子的信息时通过大儿子拷贝过来的
        Person s2 = s1.clone();
        s2.setName("小儿子");
        System.out.println(s1.getName() + " 的父亲是 " + s1.getFather().getName());
        System.out.println(s2.getName() + " 的父亲是 " + s2.getFather().getName());
    }
    // 姓名
    private String name;
    // 父亲
    private Person father;

    public Person(String _name) {
        name = _name;
    }

    public Person(String _name, Person _parent) {
        name = _name;
        father = _parent;
    }

    @Override
    public Person clone() {
        Person p = null;
        try {
            p = (Person) super.clone();
            //解决浅拷贝问题
            p.setFather(new Person(p.getFather().getName()));
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return p;

    }

    /*setter和getter方法略*/
}